﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using DeadDog;
using System.Net;
using System.IO;
using SC2parser.Model;
using SC2parser.Function;

namespace SC2parser.Parsing
{
    /// <summary>
    /// Class that gets portraits of players
    /// </summary>
    public class ImageFetcher
    {
        List<string> blacklist;
        /// <summary>
        /// Instatiates a Imagefetcher that is able to get portraits of player. Keeps a blacklist with the names that does not yield a liquipedia page
        /// </summary>
        public ImageFetcher()
        {
            blacklist = new List<string>();
            SaveLoad.Load(ref blacklist, "blacklist.dat");
        }

        private URL liquipediaPage;
        public URL LiquipediaPage
        {
            get { return liquipediaPage; }
            set { liquipediaPage = value; }
        }

        /// <summary>
        /// Gets the url to the liquipedia page of the player
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private string GetLink(string name)
        {
            LiquipediaPage = new URL("http://wiki.teamliquid.net/starcraft2/" + name);
            SCBuffer buffer = new SCBuffer("Playerbuffer");
            string source = buffer.ReadURL(liquipediaPage.Address);
            source = source.CutToFirst("<div class=\"floatnone\"><a href=\"/starcraft2/index.php?title=File:", CutDirection.Left, true);
            source = source.CutToFirst("src=\"", CutDirection.Left, true);
            source = source.CutToFirst("\"", CutDirection.Right, true);


            return "http://wiki.teamliquid.net" + source;
        }

        /// <summary>
        /// Saves the portrait of the player
        /// </summary>
        /// <param name="name">The name of a player</param>
        public void SaveImage(string name)
        {
            string link = GetLink(name);

            WebClient webclient = new WebClient();
            webclient.DownloadFile(link, name);
        }

        /// <summary>
        /// Checks if the playerportrait already existsor if the name is on the blacklist. If not it gets the picture from liquipedia
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool FetchImage(string name)
        {
            if (!blacklist.Contains(name))
            {
                if (File.Exists(name))
                {
                    return true;
                }
                else
                {
                    try
                    {
                        SaveImage(name);
                    }
                    catch (Exception)
                    {
                        blacklist.Add(name);
                        SaveLoad.Save(blacklist, "blacklist.dat");
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }
    }
}
